home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994 August: Tool Chest / Dev.CD Aug 94.toast / Sample Code / Snippets / Imaging / Graphics / icl8 To cicn / icl8 To cicn.c next >
Encoding:
C/C++ Source or Header  |  1992-12-29  |  5.0 KB  |  200 lines  |  [TEXT/KAHL]

  1. /****************************************************************************/
  2. /*                                                                            */
  3. /*    Application:    icl8 To cicn                                            */
  4. /*                                                                            */
  5. /*    Description:    This snippet converts a 'icl8' and 'ICN#' resource         */
  6. /*                    to a 'cicn' resource.                                    */
  7. /*                                                                            */
  8. /*    Files:            icl8 To cicn.π                                            */
  9. /*                    icl8 To cicn.c                                            */
  10. /*                    icl8 To cicn.π.rsrc                                        */
  11. /*                                                                            */
  12. /*    Programmer:        Edgar Lee                                                */
  13. /*    Organization:    Apple Computer, Inc.                                    */
  14. /*    Department:        Developer Technical Support, DTS                        */
  15. /*    Language:        C (Think C version 5.0.2)                                */
  16. /*    Date Created:    12-29-92                                                */
  17. /*                                                                            */
  18. /****************************************************************************/
  19.  
  20. /* Constant Declarations */
  21.  
  22. #define    WWIDTH        320
  23. #define    WHEIGHT        320
  24.  
  25. #define WLEFT        (((screenBits.bounds.right - screenBits.bounds.left) - WWIDTH) / 2)
  26. #define WTOP        (((screenBits.bounds.bottom - screenBits.bounds.top) - WHEIGHT) / 2)
  27.  
  28. /* Global Variable Definitions */
  29.  
  30. WindowPtr            gWindow;
  31. CIconHandle            gCICN;
  32.  
  33. void initMac();
  34. void createWindow();
  35. void doEventLoop();
  36.  
  37. void icl8ToCICN();
  38. void drawIcon();
  39.  
  40. main()
  41. {
  42.     initMac();
  43.     
  44.     createWindow();
  45.     icl8ToCICN();
  46.  
  47.     doEventLoop();
  48. }
  49.  
  50. void initMac()
  51. {
  52.     MaxApplZone();
  53.     
  54.     InitGraf( &thePort );
  55.     InitFonts();
  56.     InitWindows();
  57.     InitMenus();
  58.     TEInit();
  59.     InitDialogs( nil );
  60.     InitCursor();
  61.     FlushEvents( 0, everyEvent );
  62. }
  63.  
  64. void createWindow()
  65. {
  66.     Rect rect;
  67.     
  68.     SetRect( &rect, WLEFT, WTOP, WLEFT + WWIDTH, WTOP + WHEIGHT );
  69.     
  70.     gWindow = NewCWindow( 0L, &rect, "\p'icl8' To 'cicn'", true, noGrowDocProc,
  71.                             (WindowPtr)-1L, true, 0L );
  72.     SetPort( gWindow );
  73. }
  74.  
  75. void icl8ToCICN()
  76. {
  77.     Handle        icn;            /* Handle to the icon's bitmap image and mask. */
  78.     Handle        icl8;            /* Handle to the icl8 resource. */
  79.     char        depth;            /* Depth of the icl8 pixel image. */
  80.     Rect        bounds;            /* Bounding rect for the icon. */
  81.     long        bitmapSize;        /* Size of the icon's bitmap. */
  82.  
  83.     SetRect( &bounds, 0, 0, 32, 32 );    /* 'icl8' are 32x32 pixels. */
  84.     depth = 8;                            /* 8-bit deep pixel image. */
  85.     bitmapSize = 4 * 32;                /* 4 bytesPerRow * 32 rows. */
  86.     
  87.     /* Load and lock the 'icl8' and 'ICN#' resources used to build the 'cicn'. */
  88.     
  89.     icn = GetResource( 'ICN#', 128 );
  90.     icl8 = GetResource( 'icl8', 128 );
  91.     
  92.     HLock( icn );
  93.     HNoPurge( icn );
  94.     
  95.     HLock( icl8 );
  96.     HNoPurge( icl8 );
  97.         
  98.     /* Allocate memory for the 'cicn'. */
  99.     
  100.     gCICN = (CIconHandle)NewHandleClear( (long)sizeof( CIcon ) );
  101.     
  102.     /* Fill in the cicn's bitmap fields. */ 
  103.     
  104.     (**gCICN).iconBMap.baseAddr                = nil;
  105.     (**gCICN).iconBMap.rowBytes                = 4;
  106.     (**gCICN).iconBMap.bounds                = bounds;
  107.  
  108.     /* Fill in the cicn's mask bitmap fields. */
  109.     
  110.     (**gCICN).iconMask.baseAddr                = nil;
  111.     (**gCICN).iconMask.rowBytes                = 4;
  112.     (**gCICN).iconMask.bounds                = bounds;
  113.     
  114.     /* Fill in the cicn's pixmap fields. */
  115.     
  116.     (**gCICN).iconPMap.baseAddr                = nil;
  117.     (**gCICN).iconPMap.rowBytes                = (((bounds.right - bounds.left) * depth) / 8) | 0x8000;
  118.     (**gCICN).iconPMap.bounds                = bounds;
  119.     (**gCICN).iconPMap.pmVersion            = 0;
  120.     (**gCICN).iconPMap.packType                = 0;
  121.     (**gCICN).iconPMap.packSize                = 0;
  122.     (**gCICN).iconPMap.hRes                    = 72;
  123.     (**gCICN).iconPMap.vRes                    = 72;
  124.     (**gCICN).iconPMap.pixelSize            = depth;
  125.     (**gCICN).iconPMap.planeBytes            = 0;
  126.     (**gCICN).iconPMap.pmReserved            = 0;
  127.     (**gCICN).iconPMap.pixelType            = 0;
  128.     (**gCICN).iconPMap.cmpCount                = 1;
  129.     (**gCICN).iconPMap.cmpSize                = depth;
  130.     (**gCICN).iconPMap.pmTable                = GetCTable( depth );
  131.  
  132.     /* Set the 'icl8' pixel image to the iconData field. */
  133.     
  134.     (**gCICN).iconData = (Handle)icl8;
  135.     
  136.     /* Resize the 'cicn' for the bitmap image and mask. */
  137.     
  138.     SetHandleSize( gCICN, sizeof( CIcon ) + (bitmapSize * 2) );
  139.     
  140.     /* Copy the 'ICN#' data into the iconMaskData array. */
  141.     /* Note1: This is an array of shorts, so divide bitmapSize by 2. */
  142.     /* Note2: The mask comes before the image.  The is opposite of an 'ICN#' */
  143.  
  144.     BlockMove( *icn, &(**gCICN).iconMaskData[bitmapSize / 2], bitmapSize );        /* The 1bit image. */
  145.     BlockMove( *icn + (long)bitmapSize, (**gCICN).iconMaskData, bitmapSize );    /* The mask. */
  146. }
  147.  
  148. void drawIcon()
  149. {
  150.     /* Paint the background red to see that the icon's mask does work. */
  151.     
  152.     ForeColor( redColor );
  153.     PaintRect( &(*gWindow).portRect );
  154.     
  155.     /* Now, draw the 'cicn' */
  156.     
  157.     PlotCIcon( &(*gWindow).portRect, gCICN );
  158. }
  159.  
  160. void doEventLoop()
  161. {
  162.     EventRecord        event;
  163.     WindowPtr        window;
  164.     short            clickArea;
  165.     Rect            screenRect;
  166.  
  167.     for (;;)
  168.     {
  169.         if (WaitNextEvent( everyEvent, &event, 0, nil ))
  170.         {
  171.             if (event.what == mouseDown)
  172.             {
  173.                 clickArea = FindWindow( event.where, &window );
  174.                 
  175.                 if (clickArea == inDrag)
  176.                 {
  177.                     screenRect = (**GetGrayRgn()).rgnBBox;
  178.                     DragWindow( window, event.where, &screenRect );
  179.                 }
  180.                 else if (clickArea == inContent)
  181.                 {
  182.                     if (window != FrontWindow())
  183.                         SelectWindow( window );
  184.                 }
  185.                 else if (clickArea == inGoAway)
  186.                     if (TrackGoAway( window, event.where ))
  187.                         return;
  188.             }
  189.             else if (event.what == updateEvt)
  190.             {
  191.                 window = (WindowPtr)event.message;    
  192.                 SetPort( window );
  193.                 
  194.                 BeginUpdate( window );
  195.                 drawIcon();
  196.                 EndUpdate( window );
  197.             }
  198.         }
  199.     }
  200. }